package pers.lt.admin.dao;


import org.springframework.stereotype.Component;
import pers.lt.admin.dao.interfaces.LoginManagerDao;
import pers.lt.hibernatebeans.OfficesInfoEntity;
import pers.lt.hibernatebeans.UserInfoEntity;
import pers.lt.jdbc.JdbcTemplate;
import pers.lt.tools.BeanUtil;
import pers.lt.vo.UserVoOffices;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 18621 on 2016/10/14.
 */
@Component("LoginManagerDaoImp")
public class LoginManagerDaoImp implements LoginManagerDao {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public UserVoOffices queryUserInfo(String userId) {
        //String sql = " from UserInfoEntity u , OfficesInfoEntity f  where   u.officesid = f.id    u.username = ?";
        //String sql = " from UserInfoEntity u left join  OfficesInfoEntity f  where u.officesid = f.id   and    u.username = ?";
        //Query query = jdbcTemplate.getCurrentSession().createQuery(sql).setString(0,userId);
        //Query query = jdbcTemplate.getCurrentSession().createSQLQuery("");
       // List list =  query.list();
       // jdbcTemplate.queryEntitys("from UserInfoEntity where username=? ",UserInfoEntity.class);
        //jdbcTemplate.queryEntitys("from officesid where username=? ",UserInfoEntity.class);
        List<UserInfoEntity> list  = jdbcTemplate.getCurrentSession().createQuery(" from UserInfoEntity where username=?").setString(0,userId).list();
        UserInfoEntity userInfoEntity =  list.get(0);
        UserVoOffices userVoOffices = new UserVoOffices();
        BeanUtil.copyProperties(userInfoEntity, userVoOffices);
        List<OfficesInfoEntity> list1  = jdbcTemplate.getCurrentSession().createQuery(" from OfficesInfoEntity where id=?").setString(0,userVoOffices.getOfficesid()).list();
        //Map map =  new HashMap();
        if(list1.size()>0)  userVoOffices.setOfficesInfoEntity(list1.get(0));
        return userVoOffices;
    }
}
